﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Rel.Expressions
{
  public abstract partial class RegBase
    {


    
      internal bool UseExtract=true;

        public int ChainNumber { get; set; }

        internal RegBase(RelGroup parent)
        {
            Parent = parent;
            parent.Add(this);
        }

        internal RegBase()
        {
        }

        public RelGroup Parent;

        internal void AddChildExpression(RelGroup child, bool stripNames=true)
        {
            ChildExpressions.Add(child);
            
            child.UseExtractGroup = !stripNames;
            
            if (stripNames)
            {
                child.ClearGroupNames();
            }
        }

        internal List<RelGroup> ChildExpressions = new List<RelGroup>();

        public string GroupName
        {
            get { return UseExtract ? ("ExGroup" + Parent.CurrentNodeNumber.ToString() + "_" + ChainNumber.ToString()) : string.Empty; }
        }

        public string Matched;

        /// <summary>
        /// Utility method called by decorator derivatives to escape special characters.
        /// </summary>
        /// <returns>The string.</returns>
        /// <param name="s">S.</param>
        protected string EscapeString(string s)
        {

            string specialchars = @"\.$^{[(|)]}*+?";


            for (int x = 0; x < specialchars.Length; x++)
            {
                s = s.Replace(specialchars[x].ToString(), @"\" + specialchars[x]);
            }

            return s;
        }
    }
}
